package com.suixiang.wx.enterprise.suite.util;

import java.util.ArrayList;
import java.util.List;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import cn.takujo.common_api.util.AliyunOssUtil;

public class MyOssUtil {

	private static String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
	private static String ACCESS_KEY_ID = "LTAIcNRUFk8UZIpz";
	private static String ACCESS_KEY_SECRET = "r0BTPT4rYtOQdJhQKo4mKyFhz3q6pa";
	private static String BUCKET_NAME = "suixiang-wx-test";

	public static AliyunOssUtil ou = new AliyunOssUtil(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET, BUCKET_NAME);

	/**
	 * 返回前缀一样的所有key
	 * 
	 * @param prefix
	 * @return
	 */
	public static List<String> allKey(String prefix) {
		OSSClient ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
		List<String> keys = new ArrayList<String>();
		String buck = BUCKET_NAME;
		try {
			if (ossClient.doesBucketExist(buck)) {
				final int maxKeys = 200;
				String nextMarker = null;
				ObjectListing objectListing;
				do {
					ListObjectsRequest request = new ListObjectsRequest(buck);
					objectListing = ossClient
							.listObjects(request.withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys));
					List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
					for (OSSObjectSummary s : sums) {
						keys.add(s.getKey());
					}
					nextMarker = objectListing.getNextMarker();
				} while (objectListing.isTruncated());
			}
		} catch (OSSException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} finally {
			ossClient.shutdown();
		}
		return keys;
	}

}
